//
//  ScroDeleteCell.m
//  Koina
//
//  Created by welldone on 14-9-26.
//  Copyright (c) 2014年 net.koina. All rights reserved.
//

#import "ScroDeleteCell.h"
#import "config.h"
#import "Tools.h"
#import "NSString+category.h"


#define KEY_WINDOW  [[UIApplication sharedApplication]keyWindow]

#define BTN_W 45

@interface ScroDeleteCell ()<UIGestureRecognizerDelegate>

@property (nonatomic,assign)CGPoint startTouch;
@property (nonatomic,assign)int lastXpox;
@property(nonatomic,assign) BOOL isLateCell;


@end

@implementation ScroDeleteCell
@synthesize funtionBtn1,contentMoveView,startTouch,lastXpox,delegate,funtionBtn2,showFuntionBtn3,showFuntionBtn1,showFuntionBtn2,funtionBtn3;





- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        funtionBtn1  = [UIButton buttonWithType:UIButtonTypeCustom];
        [funtionBtn1 setFrame:CGRectMake(PMW-BTN_W, 0, BTN_W, BTN_W)];
        [funtionBtn1 setBackgroundImage:[Tools getImgByColor:UIColorFromRGB(0xbbac76)] forState:UIControlStateNormal];
        [funtionBtn1.titleLabel setFont:[UIFont systemFontOfSize:15]];
        
        [funtionBtn1 setAttributedTitle:[NSString makeLineStyle:@"编辑" color:[UIColor whiteColor] fontSize:15.0] forState:UIControlStateNormal];
        [funtionBtn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        funtionBtn1.tag = 101;
        [self addSubview:funtionBtn1];
        
        funtionBtn2  = [UIButton buttonWithType:UIButtonTypeCustom];
        [funtionBtn2 setFrame:CGRectMake(PMW-funtionBtn1.frame.size.width-BTN_W, 0, BTN_W, BTN_W)];
        [funtionBtn2 setBackgroundImage:[Tools getImgByColor:UIColorFromRGB(0xdb2a2b)] forState:UIControlStateNormal];
        [funtionBtn2.titleLabel setFont:funtionBtn1.titleLabel.font];
        
        [funtionBtn2 setAttributedTitle:[NSString makeLineStyle:@"禁用" color:[UIColor whiteColor] fontSize:15.0] forState:UIControlStateNormal];
        [funtionBtn2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        funtionBtn2.tag = 102;
        [self addSubview:funtionBtn2];
        
        
        
        

        funtionBtn3  = [UIButton buttonWithType:UIButtonTypeCustom];
        [funtionBtn3 setFrame:CGRectMake(PMW-funtionBtn2.frame.size.width-funtionBtn1.frame.size.width-BTN_W, 0, BTN_W, BTN_W)];
        [funtionBtn3 setBackgroundImage:[Tools getImgByColor:UIColorFromRGB(0xeca909)] forState:UIControlStateNormal];
        [funtionBtn3.titleLabel setFont:funtionBtn1.titleLabel.font];
        [funtionBtn3 setAttributedTitle:[NSString makeLineStyle:@"移动" color:[UIColor whiteColor] fontSize:15.0] forState:UIControlStateNormal];
        [funtionBtn3 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        funtionBtn3.tag = 103;
        [self addSubview:funtionBtn3];
    
        contentMoveView = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, PMW, 0)];
        [contentMoveView setBackgroundColor:[UIColor whiteColor]];
        [self addSubview:contentMoveView];
        
        self.scrollAble = YES;
        
        UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self  action:@selector(handleSwipeRight:)];
        recognizer.delegate = self;
        
        [contentMoveView  addGestureRecognizer:recognizer];
        
        self.isLateCell = NO;
    }
    return self;
}





-(void)handleSwipeRight:(UIPanGestureRecognizer*)recognizer{
    
    if (self.scrollAble==false) {
        return;
    }
    
    CGPoint touchPoint = [recognizer locationInView:KEY_WINDOW];
    
    if (recognizer.state == UIGestureRecognizerStateChanged) {
        
        [self moveViewWithX:touchPoint.x - startTouch.x];
        
    }else if (recognizer.state == UIGestureRecognizerStateBegan){
        
        startTouch = touchPoint;
        
    }else if (recognizer.state == UIGestureRecognizerStateCancelled){
        
        [UIView animateWithDuration:0.3 animations:^{
            
            [self moveViewWithX:0];
            
        } completion:^(BOOL finished) {
            
            if ([delegate respondsToSelector:@selector(MenuDidHide:)]) {
                [delegate MenuDidHide:self];
            }
            
            lastXpox = contentMoveView.frame.origin.x;
            
        }];
    }else if (recognizer.state == UIGestureRecognizerStateEnded){
    
        if (touchPoint.x - startTouch.x > 0)
        {
            [UIView animateWithDuration:0.3 animations:^{
                [self moveViewWithX:0];
                
            } completion:^(BOOL finished) {
                
                if ([delegate respondsToSelector:@selector(MenuDidHide:)]) {
                    [delegate MenuDidHide:self];
                }
                
                lastXpox = contentMoveView.frame.origin.x;
                
            }];
        }
        else
        {
            [UIView animateWithDuration:0.3 animations:^{
                [self moveViewWithX:-(funtionBtn2.frame.size.width+funtionBtn1.frame.size.width+funtionBtn3.frame.size.width)];
            } completion:^(BOOL finished) {
                
                if ([delegate respondsToSelector:@selector(menuDidShow:)]) {
                    [delegate menuDidShow:self];
                }
                
                lastXpox = contentMoveView.frame.origin.x;
                
            }];
            
        }
    }
}




- (void)moveViewWithX:(float)x
{
 
    if (x!=0) {
         x+=lastXpox;
    }
    
    x = x>20?20:x;
    x = x<-(funtionBtn2.frame.size.width+funtionBtn1.frame.size.width+funtionBtn3.frame.size.width)?-(funtionBtn2.frame.size.width+funtionBtn1.frame.size.width+funtionBtn3.frame.size.width):x;
    
   
    
    CGRect buf = contentMoveView.frame;
    buf.origin.x = x;
    [contentMoveView setFrame:buf];
    
}




-(void)setContentHight:(int)hight{

    CGRect buf = self.contentMoveView.frame;
    buf.origin.x = 0;
    buf.size.height = hight;
    [self.contentMoveView setFrame:buf];
    
    
    if (showFuntionBtn3) {
        buf = self.funtionBtn3.frame;
        buf.size.height = hight;
        [self.funtionBtn3 setFrame:buf];
    }else{
        [self.funtionBtn3 setFrame:CGRectZero];
    }
    
    
    
    if (showFuntionBtn2) {
        buf = self.funtionBtn2.frame;
        buf.size.height = hight;
        [self.funtionBtn2 setFrame:buf];
    }else{
        [self.funtionBtn2 setFrame:CGRectZero];
    }
    
    if (showFuntionBtn1) {
        buf = self.funtionBtn1.frame;
        buf.size.height = hight;
        [self.funtionBtn1 setFrame:buf];
    }else{
        [self.funtionBtn1 setFrame:CGRectZero];
    }
}



-(void)cloadMenu{
    
    
    [UIView animateWithDuration:0.3 animations:^{
        [self moveViewWithX:0];
        
    } completion:^(BOOL finished) {
        
        if ([delegate respondsToSelector:@selector(MenuDidHide:)]) {
            [delegate MenuDidHide:self];
        }
        lastXpox = contentMoveView.frame.origin.x;
    }];
}




#pragma mark UIGestureRecognizerDelegate------------------------------------------------
- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer{
    
    if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
        CGPoint velocityPoint = [gestureRecognizer velocityInView:self];
        if (fabs(velocityPoint.x) > 150&&fabs(velocityPoint.y)<150) {
            return YES;
        }else
            return NO;
    }else
        return NO;
    
}

#pragma mark 经纪人（真实客户列表中的按钮
-(void)reSetBtnNameWithStauts:(NSString *)stauts;
{
    
    [funtionBtn3 setAttributedTitle:[NSString makeLineStyle:@"绑楼盘" color:[UIColor whiteColor] fontSize:15.0] forState:UIControlStateNormal];

    
    [funtionBtn1 setAttributedTitle:[NSString makeLineStyle:@"编辑" color:[UIColor whiteColor] fontSize:15.0] forState:UIControlStateNormal];
    if ([stauts isEqualToString:@"0"])
    {
         [funtionBtn2 setAttributedTitle:[NSString makeLineStyle:@"禁用" color:[UIColor whiteColor] fontSize:15.0] forState:UIControlStateNormal];
         [funtionBtn2 setBackgroundImage:[Tools getImgByColor:UIColorFromRGB(0xdb2a2b)] forState:UIControlStateNormal];
    }
    else if([stauts isEqualToString:@"1"])
    {
        [funtionBtn2 setAttributedTitle:[NSString makeLineStyle:@"启用" color:[UIColor whiteColor] fontSize:15.0] forState:UIControlStateNormal];
         [funtionBtn2 setBackgroundImage:[Tools getImgByColor:UIColorFromRGB(0x58bf66)] forState:UIControlStateNormal];
    }
   
}


#pragma mark 经纪人（潜在客户列表中的按钮
-(void)LatentCustomerBtn:(NSString *)status;
{
    
    [funtionBtn2 setFrame:CGRectMake(PMW-funtionBtn1.frame.size.width-BTN_W*2.5, 0, BTN_W*2.5, funtionBtn2.frame.size.height)];
    [funtionBtn2 setAttributedTitle:[NSString makeLineStyle:@"转为真实客户" color:[UIColor whiteColor] fontSize:15.0] forState:UIControlStateNormal];
    [funtionBtn2 setBackgroundImage:[Tools getImgByColor:UIColorFromRGB(0xff5e5e)] forState:UIControlStateNormal];
    if ([status isEqualToString:@"1"]) {
        
        [funtionBtn3 setFrame:CGRectMake(PMW-funtionBtn2.frame.size.width-funtionBtn1.frame.size.width-BTN_W, 0, BTN_W, funtionBtn3.frame.size.height)];
        [funtionBtn3 setBackgroundImage:[Tools getImgByColor:UIColorFromRGB(0xdb2a2b)] forState:UIControlStateNormal];
        [funtionBtn3 setAttributedTitle:[NSString makeLineStyle:@"锁定" color:[UIColor whiteColor] fontSize:15.0] forState:UIControlStateNormal];
    }

}

-(void)reSetfuntionBtn2TitleWithStauts:(NSInteger)stauts;
{
    if (stauts == 0) {
        [funtionBtn2 setAttributedTitle:[NSString makeLineStyle:@"禁用" color:[UIColor whiteColor] fontSize:15.0] forState:UIControlStateNormal];
        [funtionBtn2 setBackgroundImage:[Tools getImgByColor:UIColorFromRGB(0xdb2a2b)] forState:UIControlStateNormal];
    }else if (stauts == 1)
    {
        [funtionBtn2 setAttributedTitle:[NSString makeLineStyle:@"启用" color:[UIColor whiteColor] fontSize:15.0] forState:UIControlStateNormal];
        [funtionBtn2 setBackgroundImage:[Tools getImgByColor:UIColorFromRGB(0x58bf66)] forState:UIControlStateNormal];
    }
}
@end
